package com.rock.minibatis.session.defaults;

import com.rock.minibatis.binding.MapperProxy;
import com.rock.minibatis.config.Configuration;
import com.rock.minibatis.config.JdbcProperties;
import com.rock.minibatis.mapping.MappedStatement;
import com.rock.minibatis.executor.Executor;
import com.rock.minibatis.session.SqlSession;

import java.lang.reflect.Proxy;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private final Configuration configuration;
    private final Executor executor;

    public DefaultSqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    public <E> List<E> selectList(String statement, Object parameter) {
        List rtn = null;
        try {
            MappedStatement ms = this.configuration.getMappedStatement(statement);
            rtn = this.executor.query(ms, parameter);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return rtn;
        }

    }

    public <T> T getMapper(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz},
                new MapperProxy(this,configuration));
    }
}
